<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%@ taglib prefix="bigk" uri="/bigk-tags"%>
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>

<s:if test="id == null">
    <s:set var="actionName">save</s:set>
    <s:set var="titleKey">invoicesaction.title.create</s:set>
</s:if>
<s:else>
    <s:set var="actionName">update</s:set>
    <s:set var="titleKey">invoicesaction.title.update</s:set>
</s:else>

<html>

  <head>
    <title><s:text name="invoicesaction.title"/></title>
  </head>

  <body>

    <p class="wwPageTitle">
      <img src="<s:url value='/images/invoice.gif'/>"/>
      <s:text name="%{#titleKey}"/>
    </p>

    <s:actionerror />
    <s:actionmessage/>
    <s:fielderror />

    <s:form id="invoiceInputForm"
            namespace="/invoices"
            action="%{#actionName}"
            method="POST"
            cssClass="wwFilterTable" >

      <s:hidden name="id" />

      <tr>
        <th colspan="2">
          <s:text name="invoicesaction.invoiceNo"/>
        </th>
      </tr>

      <s:label key="invoice.id"
               name="model.id"
               value="%{model.id}" />

      <s:textfield key="invoice.year"
                   name="model.year"
                   required="true"
                   size="5"/>

      <s:textfield key="invoice.number"
                   name="model.number"
                   required="true"
                   size="5"/>

      <tr>
        <th colspan="2" class="nextone">
          <s:text name="invoicesaction.termins"/>
        </th>
      </tr>

      <sx:datetimepicker key="invoice.documentDate"
                         name="model.documentDate"
                         displayFormat="dd-MM-yyyy"
                         required="true" />

      <sx:datetimepicker key="invoice.soldDate"
                         name="model.soldDate"
                         displayFormat="dd-MM-yyyy"
                         required="true"
                         valueNotifyTopics="/soldDateTopic" />

      <script type="text/javascript">
        dojo.event.topic.subscribe("/soldDateTopic", function(textEntered, date, widget){
        	dojo.event.topic.publish("/paymentDateDivReload");
        });
      </script>

      <tr>
        <td class="tdLabel">
          <s:label key="invoice.paymentDate"
                   theme="simple"
                   cssClass="label" />:
        </td>
        <td>
        
          <s:url id="paymentDateDivUrl" namespace="/nodecorate" action="PaymentDateAction" />
          <sx:div id="paymentDateDiv"
                  href="%{#paymentDateDivUrl}"
                  listenTopics="/paymentDateDivReload"
                  formId="invoiceInputForm"
                  preload="true"
                  showLoadingText="true"
                  theme="ajax" >
          </sx:div>
          
        </td>
      </tr>

      <s:select key="invoice.paymentKind"
                name="model.paymentKindId"
                required="true"
                list="paymentKindsList"
                listKey="id"
                listValue="description"
                onchange='dojo.event.topic.publish("/paymentDateDivReload")' />

      <bigk:form-table-close />
      <br/>
      
      <div class="wwPageMenu">
	    <s:url id="url" namespace="/invoices" action="createInvoicePurchaser" />
	    <a href="#"
	       class="wwDataTable"
	       onclick="document.forms[0].action='<s:property value="#url"/>'; document.forms[0].submit();">
	      <img src="<s:url value='/images/add.gif'/>"/>
	      <s:text name="invoicesaction.addInvoicePurchaser"/>
	    </a>
	  </div>
      
      <bigk:form-table-open cssClass="wwFilterTable" />

      <tr>
        <th colspan="3"><s:text name="invoice.purchasersData"/></th>
      </tr>

	  <s:iterator id="pIt" value="%{invoicePurchasers}" status="status">
      <tr>
        <td class="tdLabel">
			<s:text name="%{#pIt.purchaserRole.groupName}" />:
        </td>
        <td>
        	<s:property value="%{#pIt.nameHTML}" escape="false"/>
        	<br />
        	<s:property value="%{#pIt.addressHTML}" escape="false"/>
        	<br />
        	<s:text name="invoice.purchaserNip" />:
        	<s:property value="%{#pIt.nipHTML}" escape="false"/>
        </td>
        <td class="widthPercent1 nowrap">
          <s:url id="url" namespace="/invoices" action="editInvoicePurchaser" includeParams="none">
            <s:param name="pId" value="#status.index"/>
          </s:url>
          <s:a href="%{url}">
            <img src="<s:url value='/images/edit.gif' includeParams='none' />"
                 title="<s:text name="button.edit"/>" />
          </s:a>
          <s:url namespace="/invoices" action="deleteInvoicePurchaser" id="url" includeParams="none">
            <s:param name="pId" value="#status.index"/>
          </s:url>
          <s:text id="question4Remove" name="common.question4Remove"/>
          <s:a href="%{url}" onclick="return confirm('%{question4Remove}');">
            <img src="<s:url value='/images/delete.gif' includeParams='none' />"
                 title="<s:text name="button.deletePurchaser"/>" />
          </s:a>
        </td>
      </tr>		
	  </s:iterator>

	  <s:if test="%{invoicePurchasers.isEmpty}">
      <tr>
        <td colspan="3">
			<s:text name="invoicesaction.purchasersData.noData" />
        </td>
      </tr>
	  </s:if>

      <bigk:form-table-close />
      <br/>
      
      <div class="wwPageMenu">
	    <s:url id="url" namespace="/invoices" action="createInvoicePosition" />
	    <a href="#"
	       class="wwDataTable"
	       onclick="document.forms[0].action='<s:property value="#url"/>'; document.forms[0].submit();">
	      <img src="<s:url value='/images/add.gif'/>"/>
	      <s:text name="invoicesaction.addInvoicePosition"/>
	    </a>
	  </div>
      
      <bigk:form-table-open cssClass="wwFilterTable" />

      <tr>
        <th rowspan="2"><s:text name="invoicePosition.quantity"/></th>
        <th rowspan="2"><s:text name="invoicePosition.serviceName"/></th>
        <th rowspan="2"><s:text name="invoicePosition.symbol"/></th>
        <th rowspan="2"><s:text name="invoicePosition.measure"/></th>
        <th rowspan="2"><s:text name="invoicePosition.priceNetto"/></th>
        <th rowspan="2" class="colValueNetto"><s:text name="invoicePosition.valueNetto"/></th>
        <th colspan="2"><s:text name="invoicePosition.tax"/></th>
        <th rowspan="2" class="colTotal"><s:text name="invoicePosition.total"/></th>
        <th rowspan="2" class="widthPercent1 nowrap">&nbsp;</th>
      </tr>

      <tr>
        <th class="colInterestRate"><s:text name="invoicePosition.interestRate"/></th>
        <th class="colSum"><s:text name="invoicePosition.sum"/></th>
      </tr>
      
      <s:iterator value="model.invoicePositions" status="status">
      
      <tr>
        <td class="integer">
          <s:property value="quantity"/>
        </td>
        <td>
          <s:property value="serviceName"/>
        </td>
        <td>
          <s:property value="symbol"/>
        </td>
        <td>
          <s:property value="measure"/>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="priceNetto"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="valueNetto"/>
          </s:text>
        </td>
        <td class="integer">
          <s:text name="format.integer">
            <s:param value="interestRate"/>
          </s:text>%
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="sum"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="total"/>
          </s:text>
        </td>
        <td class="widthPercent1 nowrap">
          <s:url namespace="/invoices" action="editInvoicePosition" id="url" includeParams="none">
            <s:param name="pId" value="#status.index"/>
          </s:url>
          <s:a href="%{url}">
            <img src="<s:url value='/images/edit.gif' includeParams='none' />"
                 title="<s:text name="button.edit"/>" />
          </s:a>
          <s:url namespace="/invoices" action="deleteInvoicePosition" id="url" includeParams="none">
            <s:param name="pId" value="#status.index"/>
          </s:url>
          <s:text id="question4Remove" name="common.question4Remove"/>
          <s:a href="%{url}" onclick="return confirm('%{question4Remove}');">
            <img src="<s:url value='/images/delete.gif' includeParams='none' />"
                 title="<s:text name="button.deletePosition"/>" />
          </s:a>
        </td>
      </tr>
      
      </s:iterator>

      <%-- separator --%>
      <tr>
        <td colspan="10" class="cellUnderlined">&nbsp;</td>
      </tr>
      
      <%-- podsumowanie --%>
      <tr>
        <td colspan="4">&nbsp;</td>
        <td class="money">
          <b><s:text name="invoicePosition.allTogether"/></b>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[1.0].valueNetto"/>
          </s:text>
        </td>
        <td>
          &nbsp;
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[1.0].sum"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[1.0].total"/>
          </s:text>
        </td>
        <td class="widthPercent1 nowrap">
          &nbsp;
        </td>
      </tr>

    <s:iterator id="key" value="model.calculatedSummariesKeys" status="status">
      <tr>
        <td colspan="4">&nbsp;</td>
        <td class="money">
          <s:if test="#status.first">
            <b><s:text name="invoicePosition.allTogetherFactors"/></b>
          </s:if>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[#key].valueNetto"/>
          </s:text>
        </td>
        <td class="integer">
          <s:text name="format.integer">
            <s:param value="model.calculatedSummaries[#key].interestRate"/>
          </s:text>%
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[#key].sum"/>
          </s:text>
        </td>
        <td class="money">
          <s:text name="format.money">
            <s:param value="model.calculatedSummaries[#key].total"/>
          </s:text>
        </td>
        <td class="widthPercent1 nowrap">
          &nbsp;
          
        </td>
      </tr>
      </s:iterator>
      
      <bigk:form-table-break cssClass="wwFilterTable" />

      <tr>
        <td colspan="2" class="wwFilterTableButtonBar">
          <s:submit key="button.submit" cssClass="inputButton" theme="simple"/>
          <s:submit key="button.cancel" cssClass="inputButton" theme="simple" action="backToList" />
        </td>
      </tr>
      
    </s:form>

  </body>

</html>
